//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System.Collections;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{

	public class CategoryDetails : GalleryThemedControl
	{

		#region Child Controls

		private IText NameLabel;
		private IText CountLabel;
		private IText UpdateDateLabel;
		private IText DescriptionLabel;
		private HtmlAnchor CategoryLink;
		private HighlightPicture HighlightPicture;
		private HyperLink NextAlbum; 
		private HyperLink PreviousAlbum; 
		private Panel PagerPanel;

		#endregion

		#region Public Properties

		private PostCategory dataSource = null;
		public PostCategory DataSource
		{
			get { return dataSource; }
			set { dataSource = value; }
		}

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			NameLabel = FindText( "Name" );
			DescriptionLabel = FindText( "Description" );
			CategoryLink = (HtmlAnchor)FindControl( "CategoryLink" );
			HighlightPicture = (HighlightPicture)FindControl("HighlightPicture");
			CountLabel = FindText( "Count" );
			UpdateDateLabel = FindText( "UpdateDate" );
			NextAlbum = (HyperLink)FindControl( "NextAlbum" );
			PreviousAlbum = (HyperLink)FindControl( "PreviousAlbum" );
			PagerPanel = (Panel)FindControl ( "PagerPanel" );
		}

		#endregion

		#region Event Overrides

		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad(e);
			DataBind();
		}

		public override void DataBind()
		{
			base.DataBind();
			BindData();

		}

		#endregion

		private void BindData()
		{
			CSContext cSContext = CSContext.Current;

			if(DataSource == null)
				try
				{
					DataSource = PostCategories.GetCategory(cSContext.CategoryID, CurrentGallery.SectionID);
                    if (DataSource == null)
                        DataSource = PostCategories.GetCategories(CurrentGallery.SectionID, false, false, -1)[0] as PostCategory;
				}
				catch
				{
					this.Visible = false;
				}
			if(DataSource != null)
			{
				if(NameLabel != null)
					NameLabel.Text = DataSource.Name;
				if(DescriptionLabel != null && DataSource.Description != null)
					DescriptionLabel.Text = DataSource.Description.Replace("\n", "<br/>");
				if(CategoryLink != null)
					CategoryLink.HRef = GalleryUrls.Instance().ViewCategory(cSContext.ApplicationKey, cSContext.CategoryID);
				if(HighlightPicture != null)
					HighlightPicture.CategoryID = DataSource.CategoryID;
				if(CountLabel != null)
					CountLabel.Text = DataSource.TotalThreads.ToString() ;
				if(UpdateDateLabel != null)
					UpdateDateLabel.Text = DataSource.MostRecentPostDate.ToShortDateString();
				
				
				ArrayList categories = PostCategories.GetCategories(CurrentGallery.SectionID, false, false, -1); 
				PostCategory.SortBy = CurrentGallery.CategorySortBy;
				categories.Sort();
				int currentIndex;
				try
				{
					currentIndex = categories.IndexOf(DataSource);
				}
				catch {currentIndex=0;}

                this.Visible = (categories.Count > 1);

				if(PagerPanel != null)
					PagerPanel.Visible = (categories.Count > 1);

				if(NextAlbum != null )
				{
					if(categories.Count > 1)
					{
						int i = currentIndex + 1;
						if(i >= categories.Count)
							NextAlbum.NavigateUrl = GalleryUrls.Instance().ViewGallery(CurrentGallery.ApplicationKey);
						else
							NextAlbum.NavigateUrl = GalleryUrls.Instance().ViewCategory(CurrentGallery.ApplicationKey, ((PostCategory)categories[i]).CategoryID);
					}
					else
						NextAlbum.Visible = false;
				}

				if(PreviousAlbum != null )
				{
					if(categories.Count > 1)
					{
						int i = currentIndex - 1;
						if(i < 0)
							PreviousAlbum.NavigateUrl = GalleryUrls.Instance().ViewGallery(CurrentGallery.ApplicationKey) ;
						else
							PreviousAlbum.NavigateUrl = GalleryUrls.Instance().ViewCategory(CurrentGallery.ApplicationKey, ((PostCategory)categories[i]).CategoryID);
					} 
					else
						PreviousAlbum.Visible = false;
				}
			}
            else
            {
                this.Visible = false;
            }
			
			//Done in the nav control
			//this.SetPageTitle(DataSource.Name);
		}


	}
}
